﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Imaginecup.gameState.temp;
using Imaginecup.factory;
using Imaginecup.gameState.btnStateUtil;
using Imaginecup.sprite;
using imaginecup.util;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.util.resourceMan;
using Imaginecup.util;
using Microsoft.Xna.Framework;
using Imaginecup.util.collision;

namespace Imaginecup.gameState.option
{
    public class OptionState : GameState , ISelectBtnState
    {
        private ContentManager m_MenuContent;     

        private const int MAX_MENU = 1;

        private BtnInfo[] m_MenuButton;
        private ISprite[] m_BtnSprite;
        private DrawBtnInfo[] m_DrawBtnInfo;

        private BtnUtil m_BtnUtil;
        //컨트롤러 동일 인터페이스로 빼서 관리 고려..
        private VibeController m_VibeController;
        private SoundController m_MusicSoundController;
        private SoundController m_EffectSoundController;

        private ISprite m_BackSprite;
        private ISprite m_OptionReset;


        private EntityInfo[] m_ResetCollisionInfo;

        private static OptionState m_OptionState = null;

        private OptionState()
        {
            m_MenuContent = new ContentManager(m_Game.Services);
            m_MenuContent.RootDirectory = "Content";

            m_BtnUtil = new BtnUtil();
            m_BtnUtil.SetSelectBtnState(this);
        }



        public static OptionState GetOptionState()
        {
            if (m_OptionState == null)
            {
                m_OptionState = new OptionState();
            }
            return m_OptionState;
        }


        /************************************************************************/
        /* 초기화 되는 값(메뉴인덱스)                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            Vector2 ButtonSize = new Vector2(395, 425);

            m_MenuButton = new BtnInfo[MAX_MENU]{
                new BtnInfo(new EntityInfo( 0 , 377 , 87 , 89 )) 
            };

            m_BtnSprite = new ISprite[MAX_MENU];
            m_DrawBtnInfo = new DrawBtnInfo[MAX_MENU];

            for (int i = 0; i < MAX_MENU; i++)
            {
                m_DrawBtnInfo[i] = new DrawBtnInfo();
                m_DrawBtnInfo[i].SetBtnInfo(m_MenuButton[i]);
            }

            m_ResetCollisionInfo = new EntityInfo[]{
                new EntityInfo(505 , 390 , 170 , 90)
            };


            m_VibeController = new VibeController();
            m_EffectSoundController = new SoundController(new Vector2(256 , 191) , new EffectVolumeController());
            m_MusicSoundController = new SoundController(new Vector2(256 , 110) , new MusicVolumeController());
        }


        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)                                  */
        /************************************************************************/
        protected override void LoadStateResource()
        {
            Vector2 vibeBtnSize = new Vector2(108, 107);
            Vector2 soundBtnSize = new Vector2(76, 78);

            ResourceMan.GetResourceMan().SetTextureFrame("BackBtn", m_MenuContent.Load<Texture2D>("start_Menu\\mode_back_but"));
            ResourceMan.GetResourceMan().SetTextureFrame("Back", m_MenuContent.Load<Texture2D>("option\\option_back"));
            ResourceMan.GetResourceMan().SetTextureFrame("OptionReset", m_MenuContent.Load<Texture2D>("option\\option_reset_Back"));
            ResourceMan.GetResourceMan().SetTextureFrame("VibeBtn", m_MenuContent.Load<Texture2D>("option\\option_vibe"), vibeBtnSize);
            ResourceMan.GetResourceMan().SetTextureFrame("SoundBtn", m_MenuContent.Load<Texture2D>("option\\option_sound_but"), soundBtnSize);

           // SoundMan.GetSoundMan().LoadEffect("DefaultEffect", "sound\\malaria\\Flog");

            m_VibeController.SetSize(vibeBtnSize);
            m_MusicSoundController.SetSize(soundBtnSize);
            m_EffectSoundController.SetSize(soundBtnSize);

            SetSprite();           
            
        }

        private void SetSprite()
        {

            NormalSprite normalSprite = new NormalSprite();
            normalSprite.SetTextureName("Back");
            m_BackSprite = normalSprite;

            normalSprite = new NormalSprite();
            normalSprite.SetTextureName("OptionReset");

            m_OptionReset = normalSprite;


            ChangeSourceSizeSprite changeSprite = new ChangeSourceSizeSprite();
            changeSprite.SetSourceInfoClass(m_VibeController);
            changeSprite.SetTextureName("VibeBtn");

            m_VibeController.SetSprite(changeSprite);

            changeSprite = new ChangeSourceSizeSprite();
            changeSprite.SetSourceInfoClass(m_MusicSoundController);
            changeSprite.SetTextureName("SoundBtn");

            m_MusicSoundController.SetSprite(changeSprite);

            changeSprite = new ChangeSourceSizeSprite();
            changeSprite.SetSourceInfoClass(m_EffectSoundController);
            changeSprite.SetTextureName("SoundBtn");

            m_EffectSoundController.SetSprite(changeSprite);

            String[] btnNameList = new String[]{
                "BackBtn",
            };

            for (int i = 0; i < btnNameList.Length; i++)
            {
                ChangeSourceSizeSprite sprite = new ChangeSourceSizeSprite();
                sprite.SetTextureName(btnNameList[i]);
                sprite.SetSourceInfoClass(m_DrawBtnInfo[i]);

                m_BtnSprite[i] = sprite;
            }
        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
            for (int i = 0; i < MAX_MENU; i++)
            {
                m_BtnSprite[i].Release();
                m_DrawBtnInfo[i] = null;
            }
            m_BtnSprite = null;
            m_DrawBtnInfo = null;
            m_MenuButton = null;
            m_ResetCollisionInfo = null;

            m_VibeController.Release();
            m_MusicSoundController.Release();
            m_EffectSoundController.Release();

            m_BackSprite.Release();    
            m_MenuContent.Unload();

            ResourceMan.GetResourceMan().Release();
        }


        public override void AddStateEvent()
        {
            if (m_GameStateMan.GetTopState() != this)
            {
                this.Enabled = false;
            }
            else
                this.Enabled = true;

            base.AddStateEvent();
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {

            for (int i = 0; i < m_ResetCollisionInfo.Length; i++)
            {
                IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();
                if (mouseInput.IsOneClickUp() && Collision.CollisionByPoint(m_ResetCollisionInfo[i], mouseInput.Position))
                {
                    m_GameStateMan.PushState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_Option_Reset));
                    return;
                }
            }

            m_VibeController.Update( gameTime );
            m_EffectSoundController.Update( gameTime );
            m_MusicSoundController.Update( gameTime );

            m_BtnUtil.Update(gameTime);
            
            base.Update(gameTime);
        }

        

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = m_GraphicData.GetSpriteBatch();

            m_BackSprite.Draw(sb, new EntityInfo(0, 0, 800, 480));

            if (m_GameStateMan.GetTopState() == this)
                m_OptionReset.Draw(sb, new EntityInfo(83, 366, 655, 114));

            m_VibeController.Draw(sb);
            m_EffectSoundController.Draw(sb);
            m_MusicSoundController.Draw(sb);
            m_BtnUtil.Draw(sb);

            
            base.Draw(gameTime);
        }


        public void StateChange(int menuIndex)
        {
            switch (menuIndex)
            {
                case 0:
                    ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_TitleMenu));
                    return;                       
            }
        }

        public int GetMaxMenu()
        {
            return MAX_MENU;
        }

        public BtnInfo[] GetBtnInfoArray()
        {
            return m_MenuButton;
        }

        public ISprite[] GetSpriteArray()
        {
            return m_BtnSprite;
        }


        public DrawBtnInfo[] GetDrawBtnArray()
        {
            return m_DrawBtnInfo;
        }
    }
}
